package com.example.arithmeticleetcode.leetcode.jan;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-04 18:52
 **/
public class Demo02 {

    public static void main(String[] args) {
        int[] nums1 = {1, 1, 1};
        int[] nums2 = {1,2,2,2,5,0};
        new Demo02().waysToSplit(nums1);
        new Demo02().waysToSplit(nums2);
    }

    public int waysToSplit(int[] nums) {
        int end = nums.length - 1;
        int sum = 0;
        for (int i = 0; i <= end; i++) {
            sum += nums[i];
        }
        int first = 0;
        int second = 1;
        int count = 0;
        int len = nums.length - 2;
        while (first < len) {
            int firstSum = subArrSum(nums, 0, first);
            int secondSum = subArrSum(nums, first + 1, second);
            int thirdSum = sum - firstSum - secondSum;
            if (firstSum <= secondSum) {
                if (secondSum <= thirdSum) {
                    count++;
                } else {
                    break;
                }
            }
            if (second < end) {
                second++;
            } else {
                first++;
                second = first + 1;
            }
        }
        return count;
    }

    private int subArrSum(int[] nums, int start, int end) {
        int sum = 0;
        while (start <= end) {
            sum += nums[start++];
        }
        return sum;
    }
}
